package com.uniriho.lxr5002_service.utils;

import static android.content.Context.MODE_PRIVATE;

import android.content.Context;
import android.content.SharedPreferences;

import com.uniriho.lxr5002_service.Constants;

public class SpUtils {
    private static SharedPreferences sharedPreferences;
    private String TAG = "SpUtils";

    private SpUtils() {
    }

    private static SpUtils spUtils;
    private static Context mContext;

    public static SpUtils getInstance(Context context){
        mContext = context;
        if (spUtils == null) {
            spUtils = new SpUtils();
        }
        sharedPreferences = mContext.getSharedPreferences("PREFS_NAME", MODE_PRIVATE);
        return spUtils;
    }

    /**
     * 保存用户信息
     */
    public void saveURL(String url){
        SharedPreferences.Editor editor = sharedPreferences.edit();//获取Editor
        //得到Editor后，写入需要保存的数据
        editor.putString("url", "url");
        editor.apply();//提交修改
        LogUtils.debug(TAG,"saveURL");
    }
    /**
     * 读取用户信息
     */
    public String getURL(){
        return sharedPreferences.getString("url", Constants.BASE_RUL);
    }


    /**
     * 保存用户信息
     */
    public void saveUpdateState(int state){
        SharedPreferences.Editor editor = sharedPreferences.edit();//获取Editor
        //得到Editor后，写入需要保存的数据
        editor.putInt("upstate", state);
        editor.apply();//提交修改
        LogUtils.debug(TAG,"saveUpdateState："+state);
    }
    /**
     * 读取用户信息
     */
    public int getUpdateState(){
        return sharedPreferences.getInt("upstate", -1);
    }


    /**
     * 清空数据
     */
    public void clearUserInfo(Context context){
        SharedPreferences userInfo = context.getSharedPreferences("PREFS_NAME", MODE_PRIVATE);
        SharedPreferences.Editor editor = userInfo.edit();//获取Editor
        editor.clear();
        editor.apply();
        LogUtils.info(TAG, "clearUserInfo ");
    }
}
